﻿using SuperMap.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MDF
{
    class PrjChange
    {
        /// <summary>
        /// 初始化构造
        /// </summary>
        public PrjChange()
        {

        }

        /// <summary>
        /// 获取指定的投影信息
        /// </summary>
        /// <param name="prjCoordSys">传入投影信息属性</param>
        /// <returns></returns>
        public string GetCoordPrjInfo(PrjCoordSys prjCoordSys)
        {
            StringBuilder stringBuilder = new StringBuilder();

            try
            {
                if(prjCoordSys.Name != null)
                {
                    stringBuilder.AppendLine("投影类型：" + prjCoordSys.Name);
                }
                if(prjCoordSys.Projection != null)
                {
                    stringBuilder.AppendLine("投影方式：" + prjCoordSys.Projection.Name);
                }
                if(prjCoordSys.PrjParameter != null)
                {
                    stringBuilder.AppendLine("中央经线：" + prjCoordSys.PrjParameter.CentralMeridian.ToString("0.000000"));
                    stringBuilder.AppendLine("原点纬线：" + prjCoordSys.PrjParameter.CentralParallel.ToString());
                    stringBuilder.AppendLine("标准纬线(1)：" + prjCoordSys.PrjParameter.StandardParallel1.ToString());
                    stringBuilder.AppendLine("标准纬线(2)：" + prjCoordSys.PrjParameter.StandardParallel2.ToString());
                    stringBuilder.AppendLine("水平偏移量：" + prjCoordSys.PrjParameter.FalseEasting.ToString(".0000"));
                    stringBuilder.AppendLine("垂直偏移量：" + prjCoordSys.PrjParameter.FalseNorthing.ToString(".0000"));
                    stringBuilder.AppendLine("比例因子：" + prjCoordSys.PrjParameter.ScaleFactor.ToString());
                    stringBuilder.AppendLine("方位角：" + prjCoordSys.PrjParameter.Azimuth.ToString(".0000"));
                    stringBuilder.AppendLine("第一点经线：" + prjCoordSys.PrjParameter.FirstPointLongitude.ToString());
                    stringBuilder.AppendLine("第二点经线：" + prjCoordSys.PrjParameter.SecondPointLongitude.ToString());
                }
                if(prjCoordSys.GeoCoordSys != null)
                {
                    stringBuilder.AppendLine("地理坐标系：" + prjCoordSys.GeoCoordSys.Name);
                    stringBuilder.AppendLine("大地参照系：" + prjCoordSys.GeoCoordSys.GeoDatum.Name);
                    stringBuilder.AppendLine("参考椭球体：" + prjCoordSys.GeoCoordSys.GeoDatum.GeoSpheroid.Name);
                    stringBuilder.AppendLine("椭球长半轴：" + prjCoordSys.GeoCoordSys.GeoDatum.GeoSpheroid.Axis.ToString(".00"));
                    stringBuilder.AppendLine("椭球扁率：" + prjCoordSys.GeoCoordSys.GeoDatum.GeoSpheroid.Flatten.ToString());
                    stringBuilder.AppendLine("本初子午线：" + prjCoordSys.GeoCoordSys.GeoPrimeMeridian.LongitudeValue.ToString("0.000000"));
                }
                
            }
            catch (Exception)
            {

                throw;
            }

            return stringBuilder.ToString();
        }
        /// <summary>
        /// 更多坐标系
        /// </summary>
        public void GetMorePrjSys()
        {
            //获取所有投影坐标系名称
            string[] nameList = System.Enum.GetNames(typeof(PrjCoordSysType));
            foreach(string name in nameList)
            {

            }
        }






        /// <summary>
        /// 另存数据，执行投影转换
        /// </summary>
        /// <param name="op_dataSet">被转换的数据集</param>
        /// <param name="ed_prjCoordSys">目标投影坐标系</param>
        /// <param name="ed_dataSource">目标数据源</param>
        /// <param name="ed_setName">目标数据集的名称</param>
        /// <param name="transParameter">投影坐标系转换参数（默认）</param>
        /// <param name="transMethod">投影转换所使用的方法名称</param>
        public string TransformPrj(Dataset op_dataSet, PrjCoordSys ed_prjCoordSys, Datasource ed_dataSource, string ed_setName, CoordSysTransParameter transParameter, string transMethod)
        {
            try
            {
                //将投影坐标系名称转为枚举中具体的类型(舍弃)
                //PrjCoordSys prjCoordSys = new PrjCoordSys((PrjCoordSysType)System.Enum.Parse(typeof(PrjCoordSysType), ed_prjCoordSys));

                //将转换方法名称转为枚举中具体类型
                CoordSysTransMethod coordSysTransMethod = (CoordSysTransMethod)System.Enum.Parse(typeof(CoordSysTransMethod), transMethod);

                //执行投影变换
                Dataset ed_dataSet = CoordSysTranslator.Convert(op_dataSet, ed_prjCoordSys, ed_dataSource, ed_setName, transParameter, coordSysTransMethod);
                //确认结果
                string result = "";
                if (ed_dataSet != null)
                {
                    result = "执行成功！";
                }
                else
                {
                    result = "执行失败！";
                }

                return result;
            }
            catch (Exception)
            {

                throw;
            }
            
        }
        /// <summary>
        /// 对源数据集执行投影转换
        /// </summary>
        /// <param name="op_dataSet">被转换的数据集</param>
        /// <param name="ed_prjCoordSys">目标投影坐标系</param>
        /// <param name="transParameter">投影坐标系转换参数（默认）</param>
        /// <param name="transMethod">投影转换所使用的方法名称</param>
        /// <returns></returns>
        public string TransformPrj(Dataset op_dataSet, PrjCoordSys ed_prjCoordSys, CoordSysTransParameter transParameter, string transMethod)
        {
            try
            {
                //将投影坐标系名称转为枚举中具体的类型(舍弃)
                //PrjCoordSys prjCoordSys = new PrjCoordSys((PrjCoordSysType)System.Enum.Parse(typeof(PrjCoordSysType), ed_prjCoordSys));

                //将转换方法名称转为枚举中具体类型
                CoordSysTransMethod coordSysTransMethod = (CoordSysTransMethod)System.Enum.Parse(typeof(CoordSysTransMethod), transMethod);

                //执行投影变换
                bool output = CoordSysTranslator.Convert(op_dataSet, ed_prjCoordSys,transParameter, coordSysTransMethod);
                //确认结果
                string result = "";
                if (output)
                {
                    result = "执行成功！";
                }
                else
                {
                    result = "执行失败！";
                }

                return result;
            }
            catch (Exception)
            {

                throw;
            }
            
        }
    }
}
